/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ptr.controladores.conexionsqlserver;
import com.ptr.ptrsystem.bean.NacimientoActa;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.ptr.ptrsystem.logica.*;
import javax.servlet.http.HttpSession;
/**
 *
 * @author pavel
 */
@WebServlet(name = "ConsultasActasImagen", urlPatterns = {"/ConsultasActasImagen"})
public class ConsultasActasImagenServlet extends HttpServlet {

    public ConsultasActasImagenServlet() {
    }
Connection conn;
Statement st;
ResultSet r;
String query1, query2,query3,query4;
String CodPersona;
String nro_acta,cui_acta;
String p_ape,s_ape,p_nom;
conexionsqlserver base=new conexionsqlserver();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String bot=request.getParameter("btn1");
        CodPersona=request.getParameter("ChkDatos");
        if("BDatos".equals(bot))
        {BuscarActaDatos(request, response);}
        if("nrocui".equals(bot))
        {BuscarActaDatos(request, response);}
        ///busqueda de formulario AniDatos
        String bot2=request.getParameter("btn2");
        String seleccion=request.getParameter("Actas");
        String Tbus=request.getParameter("TipoBus");
        nro_acta=request.getParameter("nro_acta");
        cui_acta=request.getParameter("cui_acta");
        
        p_ape=request.getParameter("p_ape");
        s_ape=request.getParameter("s_ape");
        p_nom=request.getParameter("p_nom");
        
        if("DatosNAc".equals(bot2) && "0".equals(seleccion) && "0".equals(Tbus) )
        {
            out.println("<table>");
                    out.println("<tr>");
                        out.println("<td>");
                            out.println("<div>");
                                out.println("<fieldset style='border-radius: 10px 10px 10px 10px; background-color: #fff5f6;border:1px solid #f57269'>");
                                    out.println("<legend style='border-radius: 10px 10px 10px 10px; background-color: #fff5f6;border:1px solid #f57269'><img src='/RegCivil/imagenes/exclamation.png'> </legend>");
                                    out.println("Este módulo de consulta no es válida, debe seleccionar una opcion correcta!");
                                    out.println("<br> Si persiste con el problema, comuniquese con Informática, Gracias....<br>");
                                    out.println("<button type=button onclick=JavaScript:location.href='/RegCivil/jsp/AniDatos.jsp'; style='width:10.0em; heigth:8em; background-color: #eeeeee;border: 1px solid #9cafff;border-radius: 7px 7px 7px 7px;'><img src='/RegCivil/imagenes/return.png'>Retornar</button>");
                                out.println("</fieldset>");
                            out.println("</div>");
                        out.println("<td>");
                    out.println("</tr>");  
             out.println("</table>");
        }
        
        if("DatosNAc".equals(bot2) && "1".equals(seleccion) && "4".equals(Tbus) )
        {BuscaNacimientoActa(request, response);}
        if("DatosNAc".equals(bot2) && "1".equals(seleccion) && "5".equals(Tbus) )
        {BuscaNacimientoCui(request, response);}
        if("DatosNAc".equals(bot2) && "1".equals(seleccion) && "6".equals(Tbus) )
        {BuscaNacimientoDatos(request, response);}
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ConsultasActasImagenServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ConsultasActasImagenServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private boolean BuscarActaDatos(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
             base.conectar();
             conn=base.getconection();
             st = conn.createStatement();
             query1="select n.CUI_PERS,n.NRO_ACTA_PERS,n.NRO_LIBRO,";
             query1=query1+" (p.PRIM_APE_PERS+' '+ p.SEG_APE_PERS +' '+p.PRE_NOM_PERS) as datos,";
             query1=query1+" p.GEN_PERS,p.IDUBIGEO_DOMICI,p.COD_DIR, p.DIR_PERS,";
             query1=query1+" p.NRO_DIR, p.IDUBIGEO,n.FECHA_NACIMIENTO,p.EST_CIVIL_PERS,p.COD_ESTADO_PERS,";
             query1=query1+" n.RUTA_ACTA_NAC from tb_nacimiento n, tb_persona p";
             query1=query1+" where n.cod_persona=p.COD_PERSONA and n.NRO_ACTA_PERS='"+CodPersona+"'";
             r=st.executeQuery(query1);
             if(r.next())
             {
                 request.setAttribute("cui", r.getString(1));
                 request.setAttribute("nacta", r.getString(2));
                 request.setAttribute("nlibro", r.getString(3));
                 request.setAttribute("datos", r.getString(4));
                 request.setAttribute("g", r.getString(5));
                 request.setAttribute("ubidom", r.getString(6));
                 request.setAttribute("cdir", r.getString(7));
                 request.setAttribute("dper", r.getString(8));
                 request.setAttribute("nrodir", r.getString(9));
                 request.setAttribute("ubinac", r.getString(10));
                 request.setAttribute("fnac", r.getString(11));
                 request.setAttribute("ecivil", r.getString(12));
                 request.setAttribute("estpers", r.getString(13));
                 request.setAttribute("rnac", r.getString(14));
                 getServletContext().getRequestDispatcher("/jsp/VistaAni.jsp").forward(request, response);
             }
        }catch(SQLException ac)
        {
            out.println("Problemas en la busqueda por Datos"+ac.getMessage());
        }
        return false;
    }

    private void BuscaNacimientoActa(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
             base.conectar();
             conn=base.getconection();
             st = conn.createStatement();
             query2="select n.CUI_PERS,n.NRO_ACTA_PERS,n.NRO_LIBRO,";
             query2=query2+" (p.PRIM_APE_PERS+' '+ p.SEG_APE_PERS +' '+p.PRE_NOM_PERS) as datos,";
             query2=query2+" p.GEN_PERS,p.IDUBIGEO_DOMICI,p.COD_DIR, p.DIR_PERS,";
             query2=query2+" p.NRO_DIR, p.IDUBIGEO,n.FECHA_NACIMIENTO,p.EST_CIVIL_PERS,p.COD_ESTADO_PERS,";
             query2=query2+" n.RUTA_ACTA_NAC from tb_nacimiento n, tb_persona p";
             query2=query2+" where n.cod_persona=p.COD_PERSONA and n.NRO_ACTA_PERS='"+nro_acta+"'";
             r=st.executeQuery(query2);
             if(r.next())
             {
                 request.setAttribute("cui", r.getString(1));
                 request.setAttribute("nacta", r.getString(2));
                 request.setAttribute("nlibro", r.getString(3));
                 request.setAttribute("datos", r.getString(4));
                 if(r.getString(5).equals("M"))
                 {request.setAttribute("g1", "MASCULINO");}
                 else if(r.getString(5).equals("F"))
                 {request.setAttribute("g1", "FEMENINO");}
                 request.setAttribute("ubidom", r.getString(6));
                 request.setAttribute("cdir", r.getString(7));
                 request.setAttribute("dper", r.getString(8));
                 request.setAttribute("nrodir", r.getString(9));
                 request.setAttribute("ubinac", r.getString(10));
                 
                    request.setAttribute("depPN",r.getString(10).substring(0, 2));
                    request.setAttribute("ProvPN",r.getString(10).substring(2, 4));
                    request.setAttribute("DistPN",r.getString(10).substring(4, 6));
                    request.setAttribute("OtroPN",r.getString(10).substring(6, 8));
                                
                        String query3=null;
                        String nubinac=null;
                        Connection c;
                        ResultSet rs;
                        try
                        {
                            base.conectar(); c=base.getconection();
                            st=c.createStatement();
                            query3="select (u.nom_ubigeo_dep +' / '+ u.nom_ubigeo_prov+' / '";
                            query3=query3+"+u.nom_ubigeo_dist +' / '+ u.nom_ubigeo_dist) as nom_ubigeo from TB_UBIGEO u";
                            query3=query3+" where idubigeo='"+r.getString(10)+"'";
                            rs=st.executeQuery(query3);
                            if(rs.next())
                            {
                                request.setAttribute("nubinac",rs.getString(1));
                            }
                        }catch(SQLException ex)    
                        {
                            System.out.println("Existe erron en la busqueda de Ubigeo :" + ex.getMessage());
                        }
                 
                 
                 request.setAttribute("fnac", r.getString(11));
                                    if(r.getString(12).equals("S"))
                                    {request.setAttribute("civil","SOLTERO");}
                                    else if(r.getString(12).equals("C"))
                                    {request.setAttribute("civil","CASADO");}
                                    else if(r.getString(12).equals("D"))
                                    {request.setAttribute("civil","DIVORCIADO");}
                                    else if(r.getString(12).equals("V"))
                                    {request.setAttribute("civil","VIUDO");}
                 request.setAttribute("estpers", r.getString(13));
                 request.setAttribute("rnac", r.getString(14));
                 getServletContext().getRequestDispatcher("/jsp/AniDatos.jsp").forward(request, response);
             }
        }catch(SQLException ac)
        {
            out.println("Problemas en la busqueda por Datos"+ac.getMessage());
        }
    }

    private void BuscaNacimientoCui(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
         try
        {
             base.conectar();
             conn=base.getconection();
             st = conn.createStatement();
             query3="select n.CUI_PERS,n.NRO_ACTA_PERS,n.NRO_LIBRO,";
             query3=query3+" (p.PRIM_APE_PERS+' '+ p.SEG_APE_PERS +' '+p.PRE_NOM_PERS) as datos,";
             query3=query3+" p.GEN_PERS,p.IDUBIGEO_DOMICI,p.COD_DIR, p.DIR_PERS,";
             query3=query3+" p.NRO_DIR, p.IDUBIGEO,n.FECHA_NACIMIENTO,p.EST_CIVIL_PERS,p.COD_ESTADO_PERS,";
             query3=query3+" n.RUTA_ACTA_NAC from tb_nacimiento n, tb_persona p";
             query3=query3+" where n.cod_persona=p.COD_PERSONA and n.CUI_PERS='"+cui_acta+"'";
             r=st.executeQuery(query3);
             if(r.next())
             {
                 request.setAttribute("cui", r.getString(1));
                 request.setAttribute("nacta", r.getString(2));
                 request.setAttribute("nlibro", r.getString(3));
                 request.setAttribute("datos", r.getString(4));
                 if(r.getString(5).equals("M"))
                 {request.setAttribute("g1", "MASCULINO");}
                 else if(r.getString(5).equals("F"))
                 {request.setAttribute("g1", "FEMENINO");}
                 request.setAttribute("ubidom", r.getString(6));
                 request.setAttribute("cdir", r.getString(7));
                 request.setAttribute("dper", r.getString(8));
                 request.setAttribute("nrodir", r.getString(9));
                 request.setAttribute("ubinac", r.getString(10));
                 
                    request.setAttribute("depPN",r.getString(10).substring(0, 2));
                    request.setAttribute("ProvPN",r.getString(10).substring(2, 4));
                    request.setAttribute("DistPN",r.getString(10).substring(4, 6));
                    request.setAttribute("OtroPN",r.getString(10).substring(6, 8));
                                
                        String query3=null;
                        String nubinac=null;
                        Connection c;
                        ResultSet rs;
                        try
                        {
                            base.conectar(); c=base.getconection();
                            st=c.createStatement();
                            query3="select (u.nom_ubigeo_dep +' / '+ u.nom_ubigeo_prov+' / '";
                            query3=query3+"+u.nom_ubigeo_dist +' / '+ u.nom_ubigeo_dist) as nom_ubigeo from TB_UBIGEO u";
                            query3=query3+" where idubigeo='"+r.getString(10)+"'";
                            rs=st.executeQuery(query3);
                            if(rs.next())
                            {
                                request.setAttribute("nubinac",rs.getString(1));
                            }
                        }catch(SQLException ex)    
                        {
                            System.out.println("Existe erron en la busqueda de Ubigeo :" + ex.getMessage());
                        }
                 
                 
                 request.setAttribute("fnac", r.getString(11));
                 //request.setAttribute("ecivil", r.getString(12));
                                    if(r.getString(12).equals("S"))
                                    {request.setAttribute("civil","SOLTERO");}
                                    else if(r.getString(12).equals("C"))
                                    {request.setAttribute("civil","CASADO");}
                                    else if(r.getString(12).equals("D"))
                                    {request.setAttribute("civil","DIVORCIADO");}
                                    else if(r.getString(12).equals("V"))
                                    {request.setAttribute("civil","VIUDO");}
                 request.setAttribute("estpers", r.getString(13));
                 request.setAttribute("rnac", r.getString(14));
                 getServletContext().getRequestDispatcher("/jsp/AniDatos.jsp").forward(request, response);
             }
        }catch(SQLException ac)
        {
            out.println("Problemas en la busqueda por Datos"+ac.getMessage());
        }
    }

    private void BuscaNacimientoDatos(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
         try
        {
             base.conectar();
             conn=base.getconection();
             st = conn.createStatement();
             query4="select n.CUI_PERS,n.NRO_ACTA_PERS,n.NRO_LIBRO,";
             query4=query4+" (p.PRIM_APE_PERS+' '+ p.SEG_APE_PERS +' '+p.PRE_NOM_PERS) as datos,";
             query4=query4+" p.GEN_PERS,p.IDUBIGEO_DOMICI,p.COD_DIR, p.DIR_PERS,";
             query4=query4+" p.NRO_DIR, p.IDUBIGEO,n.FECHA_NACIMIENTO,p.EST_CIVIL_PERS,p.COD_ESTADO_PERS,";
             query4=query4+" n.RUTA_ACTA_NAC from tb_nacimiento n, tb_persona p";
             query4=query4+" where n.cod_persona=p.COD_PERSONA";
             query4=query4+" and p.PRIM_APE_PERS='"+p_ape+"'";
             query4=query4+" and p.SEG_APE_PERS='"+s_ape+"'";
             query4=query4+" and p.PRE_NOM_PERS='"+p_nom+"'";
             r=st.executeQuery(query4);
             if(r.next())
             {
                 request.setAttribute("cui", r.getString(1));
                 request.setAttribute("nacta", r.getString(2));
                 request.setAttribute("nlibro", r.getString(3));
                 request.setAttribute("datos", r.getString(4));
                 if(r.getString(5).equals("M"))
                 {request.setAttribute("g1", "MASCULINO");}
                 else if(r.getString(5).equals("F"))
                 {request.setAttribute("g1", "FEMENINO");}
                 request.setAttribute("ubidom", r.getString(6));
                 request.setAttribute("cdir", r.getString(7));
                 request.setAttribute("dper", r.getString(8));
                 request.setAttribute("nrodir", r.getString(9));
                 request.setAttribute("ubinac", r.getString(10));
                 
                    request.setAttribute("depPN",r.getString(10).substring(0, 2));
                    request.setAttribute("ProvPN",r.getString(10).substring(2, 4));
                    request.setAttribute("DistPN",r.getString(10).substring(4, 6));
                    request.setAttribute("OtroPN",r.getString(10).substring(6, 8));
                                
                        String query3=null;
                        String nubinac=null;
                        Connection c;
                        ResultSet rs;
                        try
                        {
                            base.conectar(); c=base.getconection();
                            st=c.createStatement();
                            query3="select (u.nom_ubigeo_dep +' / '+ u.nom_ubigeo_prov+' / '";
                            query3=query3+"+u.nom_ubigeo_dist +' / '+ u.nom_ubigeo_dist) as nom_ubigeo from TB_UBIGEO u";
                            query3=query3+" where idubigeo='"+r.getString(10)+"'";
                            rs=st.executeQuery(query3);
                            if(rs.next())
                            {
                                request.setAttribute("nubinac",rs.getString(1));
                            }
                        }catch(SQLException ex)    
                        {
                            System.out.println("Existe erron en la busqueda de Ubigeo :" + ex.getMessage());
                        }
                 request.setAttribute("fnac", r.getString(11));
                                    if(r.getString(12).equals("S"))
                                    {request.setAttribute("civil","SOLTERO");}
                                    else if(r.getString(12).equals("C"))
                                    {request.setAttribute("civil","CASADO");}
                                    else if(r.getString(12).equals("D"))
                                    {request.setAttribute("civil","DIVORCIADO");}
                                    else if(r.getString(12).equals("V"))
                                    {request.setAttribute("civil","VIUDO");}
                 request.setAttribute("estpers", r.getString(13));
                 request.setAttribute("rnac", r.getString(14));
                 getServletContext().getRequestDispatcher("/jsp/AniDatos.jsp").forward(request, response);
             }
        }catch(SQLException ac)
        {
            out.println("Problemas en la busqueda por Datos"+ac.getMessage());
        }
    }
}
